# Maintainer: Wireshark Core Team <wireshark-dev@wireshark.org>

_realname=wireshark
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=4.1.0rc0.1408.gd00bd20fea
pkgrel=1
pkgdesc="Network traffic and protocol analyzer/sniffer"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url="https://www.wireshark.org"
license=('GPL2')
depends=("${MINGW_PACKAGE_PREFIX}-bcg729"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-libgcrypt"
         "${MINGW_PACKAGE_PREFIX}-libilbc"
         "${MINGW_PACKAGE_PREFIX}-libmaxminddb"
         "${MINGW_PACKAGE_PREFIX}-libpcap"
         "${MINGW_PACKAGE_PREFIX}-libsmi"
         "${MINGW_PACKAGE_PREFIX}-libssh"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-minizip"
         "${MINGW_PACKAGE_PREFIX}-nghttp2"
         "${MINGW_PACKAGE_PREFIX}-opencore-amr"
         "${MINGW_PACKAGE_PREFIX}-opus"
         "${MINGW_PACKAGE_PREFIX}-pcre2"
         "${MINGW_PACKAGE_PREFIX}-qt6-base"
         "${MINGW_PACKAGE_PREFIX}-qt6-multimedia"
         "${MINGW_PACKAGE_PREFIX}-qt6-5compat"
         "${MINGW_PACKAGE_PREFIX}-sbc"
         "${MINGW_PACKAGE_PREFIX}-snappy"
         "${MINGW_PACKAGE_PREFIX}-spandsp"
         "${MINGW_PACKAGE_PREFIX}-speexdsp"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools"
             "${MINGW_PACKAGE_PREFIX}-asciidoctor"
             "${MINGW_PACKAGE_PREFIX}-docbook-xsl"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             'git')
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest"
              "${MINGW_PACKAGE_PREFIX}-python-pytest-xdist")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
_commit=${PKG_COMMIT:-HEAD}
source=("${_realname}"::"git+https://gitlab.com/wireshark/wireshark.git#commit=${_commit}")
sha256sums=('SKIP')

pkgver() {
  cd "${_realname}"

  git describe --long "${_commit}" | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g;s/^v//g'
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DENABLE_WERROR=OFF \
      -DUSE_qt6=ON \
      -DENABLE_WINSPARKLE=OFF \
      -DFETCH_lua=ON \
      ../${_realname}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
  "${MINGW_PREFIX}"/bin/cmake.exe --build . --target user_guide_html
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  "${MINGW_PREFIX}"/bin/cmake.exe --build . --target test-programs
  "${MINGW_PREFIX}"/bin/pytest.exe
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install . --component UserGuide
  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install . --component Development

  install -Dm644 "${srcdir}/${_realname}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
